package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: BrandController
 * Package: com.spzx.product.controller
 * Description:
 *
 * @Author ycy
 * @Create 2024/7/27 15:41
 * @Version 1.0
 */
@Tag(name = "品牌接口管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌列表
     */
    @RequiresPermissions("product:brand:list")
    @Operation(summary = "查询品牌列表")
    @GetMapping(value="list")
    // 返回分页信息的 用 TableDataInfo
    public TableDataInfo list(Brand brand){
        // 若依框架中的BaseController提供的分页工具
        startPage();

        List<Brand> list =  brandService.selectBrandList(brand);


        return  getDataTable(list);
    }

    /**
     * 获取品牌详情信息
     *
     */
    @RequiresPermissions("product:brand:query")
    @Operation(summary = "获取品牌详情信息")
    @GetMapping(value="/{id}")
    // 返回实体类的 用  AjaxResult
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(brandService.selectBrandById(id));
    }

    /**
     * 新增品牌
     */
    @Log(title="品牌管理",businessType = BusinessType.INSERT)
    @RequiresPermissions("product:brand:add")
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     *
     * 修改品牌
     */
    @Log(title = "品牌管理",businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:brand:edit")
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌
     */
    @Log(businessType = BusinessType.DELETE)
    @RequiresPermissions("product:brand:remove")
    @Operation(summary = "删除品牌")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult remove(@PathVariable Long [] ids ){
        return toAjax(brandService.deleteBrandById(ids));
    }

    /**
     * 获取全部品牌
     */
    @RequiresPermissions("product:brand:query")
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAll")
    public AjaxResult getBrandAll(){
        return success(brandService.selectBrandAll());
    }

    @InnerAuth//表示内部调用的接口，能被外部请求访问？
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList(){
        return R.ok(brandService.selectBrandAll());
    }
}
